package tt.dz.appserver.util.resource;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.util.Constants;

/**
 * 资源文件读取工具类 xielf
 */
public class ResourceUtils implements Serializable {


	/***/
	private static final long serialVersionUID = -8798929588073623754L;
	

	/**
	 * 根据路径得到资源文件中的内容
	 * 
	 * @param filePath
	 *            文件路径
	 * @return
	 */
	public static final String getRootResource(String filePath) {

		BufferedReader reader = null;
		FileInputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		try {
			// 获取文件对象
			File file = new File(
					URLDecoder.decode(ResourceUtils.class.getClassLoader().getResource(filePath).getPath(), Constants.CHARSET));
			
			//流相关
			inputStream = new FileInputStream(file);
			inputStreamReader = new InputStreamReader(inputStream, Constants.CHARSET);
			reader = new BufferedReader(inputStreamReader);
			
			String readStr = null;
			StringBuffer allStr = new StringBuffer();
			// 读取里面的内容
			while ((readStr = reader.readLine()) != null) {
				allStr.append(readStr);
			}
			return JSONObject.parseObject(allStr.toString()).toJSONString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			// 关闭读取器，必须
			if (reader != null) {
				try {
					reader.close();
					inputStreamReader.close();
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 根据语言、国家、资源文件名和key名字获取资源文件值
	 * 
	 * @param propertyName
	 *            资源文件名
	 * @param key
	 *            key名字
	 * @return 值
	 */
	public final static String getProperties(String propertyName, String key) {
		String retValue = "";
		try {
			ResourceBundle rb = ResourceBundle.getBundle(propertyName, Locale.CHINA);
			retValue = (String) rb.getObject(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retValue;
	}

	/**
	 * 获取所有的key集合
	 * 
	 * @param baseName
	 * @return
	 */
	public final static List<String> getkeyList(String baseName) {

		ResourceBundle rb = ResourceBundle.getBundle(baseName, Locale.CHINA);

		List<String> reslist = new ArrayList<String>();

		for (Iterator<String> it = rb.keySet().iterator(); it.hasNext();) {
			String lkey = (String) it.next();
			reslist.add(lkey);
		}
		return reslist;

	}

	/**
	 * 通过key从资源文件读取内容，并格式化
	 * 
	 * @param fileName
	 *            资源文件名
	 * 
	 * @param key
	 *            索引
	 * 
	 * @param objs
	 *            格式化参数
	 * 
	 * @return 格式化后的内容
	 */
	public final static String getValue(String propertyName, String key, Object[] objs) {
		String pattern = getProperties(propertyName, key);
		String value = MessageFormat.format(pattern, objs);
		return value;
	}
	public static void main(String[] args) {
		System.out.println(ResourceUtils.getRootResource("avro/app/appServer.json"));
	}
}
